package com.example.mqttxprovider.controller;

import com.example.mqttxprovider.callback.MqttConsumerCallBack;
import com.example.mqttxprovider.config.MqttConsumerConfig;
import com.example.mqttxprovider.service.MessageService;
import com.example.mqttxprovider.vo.ReturnVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class MqttConsumerController {
    @Resource
    private MqttConsumerConfig client;
    @Resource
    private MessageService messageService;

    private MqttConsumerCallBack callBack = new MqttConsumerCallBack();

    @Value("${spring.mqtt.client.id}")
    private String clientId;

    @RequestMapping("/connect")
    public String connect(){
        client.connect();
        return clientId + "连接到服务器";
    }

    @RequestMapping("/disConnect")
    public String disConnect(){
        client.disConnect();
        return client + "与服务器断开连接";
    }

    @RequestMapping("/home/greenhouse/temperature")
    public ReturnVO temperature(){
        String topic = "home/greenhouse/temperature";
        String msg = callBack.getMesByTopic(topic);

        ReturnVO returnVO = new ReturnVO(msg.substring(3, 9), msg.substring(15, 20), null);
        return returnVO;
    }

    @RequestMapping(value = "/home/greenhouse/soil_moisture" ,method = RequestMethod.GET)
    public ReturnVO soil_moisture(){
        String topic = "home/greenhouse/soil_moisture";
        String msg = callBack.getMesByTopic(topic);
        ReturnVO returnVO = new ReturnVO();
        returnVO.setSoilMoisture(msg);

        return returnVO;
    }

    @RequestMapping("/home/greenhouse/humidity")
    public String humidity(){
        String topic = "home/greenhouse/humidity";
        String msg = callBack.getMesByTopic(topic);
        return msg;
    }

    @RequestMapping("/test/taspberry")
    public String taspberry(){
        String topic = "test/taspberry";
        String msg = callBack.getMesByTopic(topic);
        return msg;
    }
}
